package com.jinghua.treatment.controller;

import com.jinghua.treatment.common.GlobalConstant;
import com.jinghua.treatment.common.domain.JSONResult;
import com.jinghua.treatment.entity.SysDoctorNoticeSetting;
import com.jinghua.treatment.service.SysNoticeSettingService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 公告及主页控制器
 * Created by 王兴光 on 2018/3/30.
 */
@Controller
@RequestMapping("/notice")
public class SysNoticeController {
    @Resource
    private SysNoticeSettingService noticeSettingService;

    @ApiOperation(value="添加公告及主页信息")
    @RequestMapping(value="/insertNotice",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JSONResult insertNotice(Integer doctorId){
        System.out.println("doctorId:"+doctorId);
        SysDoctorNoticeSetting notice = null;
        // 验证数据库是否有该医生的公告信息
        notice = noticeSettingService.selectByDoctorId(doctorId);
        if (notice!=null){
            System.out.println("------已创建---------");
            return new JSONResult(GlobalConstant.SUCCESS_CODE,"已创建过",notice);
        }else {
            // 添加公告信息
            notice = new SysDoctorNoticeSetting();
            notice.setDoctorId(doctorId);
            System.out.println("----------添加公告信息-----------");
            noticeSettingService.insert(notice);
            return new JSONResult(GlobalConstant.SUCCESS_CODE, "创建成功");
        }
    }

    @ApiOperation(value="根据医生编号，获取公告及主页信息")
    @GetMapping(value="/getNoticeById")
    @ResponseBody
    public SysDoctorNoticeSetting findNoticeById(Integer doctorId){return noticeSettingService.selectByDoctorId(doctorId);}

    @ApiOperation(value="修改公告及主页信息")
    @RequestMapping(value="/updateNotice",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JSONResult updateNotice(Integer doctorId,String noticeContent,String introduce,String specialty,String description,
                                   Double chargeFee,String chargeDesc){
        System.out.println("content:"+noticeContent);

        // 根据医生编号获取公告对象
        SysDoctorNoticeSetting notice = noticeSettingService.selectByDoctorId(doctorId);
        // 设置公告
        if(noticeContent!=null)
            notice.setNotice(noticeContent);
        // 设置简介
        if(introduce!=null)
            notice.setIntroduce(introduce);
        // 设置擅长
        if(specialty!=null)
            notice.setGoodAt(specialty);
        // 设置描述，欢迎语
        if(description!=null)
            notice.setWelcomeWords(description);
        // 设置收费金额
        if(chargeFee!=null)
            notice.setChargeFee(chargeFee);
        // 设置收费标准说明
        if(chargeDesc!=null)
            notice.setChargeDesc(chargeDesc);
        noticeSettingService.updateByPrimaryKey(notice);
        return  new JSONResult(GlobalConstant.SUCCESS_CODE, "公告信息修改成功");
    }

}
